﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara.Location;
using System.Data;

public partial class User_Controls_Modal_SearchLocation : System.Web.UI.UserControl
{
    #region /* Public Events */
    public delegate void LocationSelectionEventHandler(object sender, LocationSelectionArgs e);
    public event LocationSelectionEventHandler LocationSelected;
    public event EventHandler Closed;
    #endregion

    ILocationActions LocationAction = new Location_Controller();


    protected void XRCLocationSearch_Closing(object sender, EventArgs e)
    {
        if (Closed != null)
        {
            Closed(this, e);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void XRCBSearchLocation_Click(object sender, EventArgs e)
    {
        GridViewLocations.DataSource = LocationAction.Search_Locations(TextBoxSearchLocation.Text);
        GridViewLocations.DataBind();
    }
    protected void XRCBAddNewExternalLocation_Click(object sender, EventArgs e)
    {
        XRCLocationSearch.Hide();
        XRCAddNewExternalLocation.Show();
    }
    protected void XRCBSaveNewExternalLocation_Click(object sender, EventArgs e)
    {
        Location LocationNewLocation = new Location();
        LocationNewLocation.Name = TextBoxNameToDisplay.Text.ToString();
        LocationNewLocation.Address1 = TextBoxAddress1.Text.ToString();
        LocationNewLocation.Address2 = TextBoxAddress2.Text.ToString();
        LocationNewLocation.Address3 = TextBoxAddress3.Text.ToString();
        LocationNewLocation.Postcode = TextBoxPostcode.Text.ToString();

        Location_Controller Controller = new Location_Controller();
        ILocation LocationInserted = Controller.InsertLocation(LocationNewLocation);
        TriggerSelectionEvent(sender, int.Parse(LocationInserted.AdditionalField1), LocationInserted.ID,LocationInserted.IsInternal,LocationInserted.ToString());
    }
    public void Show()
    {
        XRCLocationSearch.Show();
    }
    protected void GridViewLocations_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        TriggerSelectionEvent(sender,
            int.Parse(GridViewLocations.DataKeys[e.NewSelectedIndex][1].ToString()),
            int.Parse(GridViewLocations.DataKeys[e.NewSelectedIndex][0].ToString()),
            (bool)GridViewLocations.DataKeys[e.NewSelectedIndex][2],
            GridViewLocations.DataKeys[e.NewSelectedIndex][3].ToString()+(
            string.IsNullOrEmpty(GridViewLocations.DataKeys[e.NewSelectedIndex][4].ToString()) ? "" : " - " + GridViewLocations.DataKeys[e.NewSelectedIndex][4].ToString())
            );
        this.Hide();
    }
    private void TriggerSelectionEvent(object sender,int RoomId,int LocationId,bool isInternal,string Location)
    {
        LocationSelectionArgs ArgsToSend = new LocationSelectionArgs();
        ArgsToSend.LocationID = LocationId;
        ArgsToSend.RoomID = RoomId;
        ArgsToSend.isInternal = isInternal;
        ArgsToSend.Location = Location;

        if (LocationSelected != null)
        {
            LocationSelected(sender, ArgsToSend);
        }
        else
        {
            throw new Exception("Silly fool implement the 'OnLocationSelected' event!!");
        }
    }
    public class LocationSelectionArgs : EventArgs
    {
        public int LocationID;
        public int RoomID;
        public string Location;
        public bool isInternal;
    }

    public void ClearFields()
    {
        throw new NotImplementedException();
    }

    public void Hide()
    {
        XRCLocationSearch.Hide();
    }
}